<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 购物车
 * @author www.dingwei.cn
 */

class ConsumerCartModel extends MY_Model{
	
	public $tbShoppingCart = 'consumer_cart'; //购物车
	public $tbProduct = 'product';
	public $tbProductVariant= 'product_variant';//标签属性组合
	
	
	/**
	 * 获取购物车中的产品信息
	 * @param integer $productId 产品id
	 * @param integer $consumerId 消费者id
	 * @return array|array
	 * @author zyl
	 */
	public function getCartProductInfo($productId,$sort,$consumerId){
		
		if ( ! $productId || ! $consumerId || ! $sort){
			return array();
		}
		
		$this->db
		->select('id,variant_id,quantity')
		->where('consumer_id',$consumerId)
		->where('product_id',$productId)
		->where('variant_id',$sort);
		
		return $this->getOne($this->tbShoppingCart);
	}
	
	
	/**
	 * 获取购物车商品数量（商品在数据库中的条数，不是数量总和）
	 * @param 	integer $consumerId
	 * @return	integer
	 */
	public function getProductCount($consumerId){
		
		$this->db->where('consumer_id', $consumerId);
		return $this->count($this->tbShoppingCart);
	}

	
	/**
	 * 每个用户用户的购物车产品种类的上限数量
	 * @return	integer
	 */
	public function maxCartProductCount(){
		return getCustomConfig('max_cart_product_count');
	}
	
	/**
	 * 获取产品在购物车中数量
	 * @param 	integer $userId
	 * @param 	integer $productId
	 * @param 	integer $variantId
	 * @return	integer
	 */
	public function getQuantity($userId, $productId, $variantId){
		
		$this->db->select('quantity')
				 ->where([
				 		  'consumer_id' => $userId,
				 		  'product_id' => $productId
				 ]);
		if($variantId){
			$this->db->where('variant_id', $variantId);
		}			 
	
		$tA = $this->getOne($this->tbShoppingCart);
		if($tA){
			return $tA['quantity'];
		}
		
		return 0;
	}
	
	/**
	 * 获取购物车商品
     * @param $userId
     * @param int $merchantId
     * @param int $product_id
	 * @return	integer
	 */
	public function getList($userId,$merchantId = 0, $product_id = 0){
		
		if ($merchantId){
			$this->db->where('p.merchant_id',$merchantId);
		}
		if ($product_id){
            $this->db->where('c.product_id',$product_id);
        }
		$this->db->select('p.id,c.id as cart_id,c.product_id,p.icon,p.merchant_id,p.goods_price,p.market_price,p.status,
		                p.limit_number,p.discount,v.price,v.sort,p.name,v.property,c.quantity,p.lunch_box_fee,c.tags')
				->join($this->tbProduct.' p', 'p.id = c.product_id')
				->join($this->tbProductVariant.' v', 'v.product_id = p.id and v.sort = c.variant_id', 'left')
				->where('c.consumer_id', $userId);
//				->order_by('c.addtime desc');
		
		$data = array();
		$tA = $this->getAll($this->tbShoppingCart.' c');
		foreach ($tA as $k=>$v){
			
			if( ! isset($data[$v['merchant_id']])){
				$data[$v['merchant_id']] = [];
			}

			$merchantId = $v['merchant_id'];
			unset($v['merchant_id']);
			$v['icon'] = getImage($v['icon'], 'xs');
			$v['property'] = json_decode($v['property'], true);
			$v['price'] = str_replace('.00', '', $v['price']);
			$v['market_price'] = str_replace('.00', '', $v['market_price']);
			$v['goods_price'] = $v['price'] ? $v['price']:$v['goods_price'];
			if( ! is_array($v['property'])){
				$v['property'] = [];
				$v['price'] = str_replace('.00', '', $v['goods_price']);
			}
			$v['sort'] = empty($v['sort'])?"0":$v['sort'];
            if($v['tags']){
                $v['tags'] = rtrim($v['tags'],'|');//去掉最后一个字符
                $v['tags'] = ltrim($v['tags'],'|');//去掉第一个字符

            }
            //限购活动
            if($v['limit_number'] > 0 && $v['discount'] > 0){
                $v['price'] = sprintf('%.2f',$v['price']*$v['discount']/10);
            }
			unset($v['goods_price']);
			$data[$merchantId][] = $v;

		}

		return $data;
	}

	/**
     * 删除购物车商品
     */
	public function deleteCartProduct($consumerId, $product_id){
	    if($product_id){
	        $this->db->where('product_id', $product_id);
        }
        $this->db->where('consumer_id', $consumerId);

        return $this->delete($this->tbShoppingCart);
    }
}